﻿
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports requestProcessors_share

Public Class status
    Inherits controlProcessor

    Public Const _pathidentity As String = network.pathSeperator + "status"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Private Shared Function combine(ByVal name As String, ByVal value As Int64) As String
        Return "(" + name + ", " + Convert.ToString(value) + ")"
    End Function

    Private Shared Sub addcountersInfo(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")
        Dim i As Int64
        For i = 0 To counter.countersCount() - 1
            Dim name As String = Nothing
            Dim count As Int64 = npos
            Dim average As Int64 = npos
            Dim lastAverage As Int64 = npos
            Dim rate As Int64 = npos
            Dim lastRate As Int64 = npos
            If counter.counter(i, name, count, average, lastAverage, rate, lastRate) Then
                addkeyvaluepair(context, root, name, _
                                combine("count", count) + ", " + _
                                combine("average", average) + ", " + _
                                combine("lastAverage", lastAverage) + ", " + _
                                combine("rate", rate) + ", " + _
                                combine("lastRate", lastRate))
            Else
                addkeyvaluepair(context, root, "CANNOT_GET_COUNTER_INFO", Convert.ToString(i))
            End If
        Next
    End Sub

    Private Shared Sub addIpsInfo(ByVal context As httpContext, ByVal root As xmlcontrol, _
                                  ByVal host As String, ByVal ips As vector(Of String))
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")
        assert(Not ips Is Nothing, "ips is nothing.")

        Dim msg As div = Nothing
        msg = context.createControl(New div())
        msg.text = "for host " + host
        root.addchild(msg)

        Dim i As Int64
        For i = 0 To ips.size() - 1
            msg = context.createControl(New div())
            msg.text = Convert.ToString(ips(i))
            root.addchild(msg)
        Next
    End Sub

    Private Shared Sub addpublicIpsInfo(ByVal context As httpContext, ByVal root As xmlcontrol)
        addIpsInfo(context, root, context.url().host, context.publicIps())
    End Sub

    Private Shared Sub addoriginalIpsInfo(ByVal context As httpContext, ByVal root As xmlcontrol)
        addIpsInfo(context, root, requestHost(context.request), publicIps(context.request()))
    End Sub

    Public Shared Function createXHtmlPage(ByVal context As httpContext) As xhtmlpage
        assert(Not context Is Nothing, "context is nothing.")
        Dim rtn As xhtmlpage = Nothing
        rtn = context.createControl(New xhtmlpage())
        rtn.title().text = "http server status"

        Dim ul As ul = Nothing
        ul = context.createControl(New ul())
        rtn.body().addchild(ul)

        Dim li As li = Nothing
        li = context.createControl(New li())
        ul.addchild(li)
        Dim xmltext As xmltext = Nothing
        xmltext = context.createControl(New xmltext())
        xmltext.text = "counters info"
        li.addchild(xmltext)
        addcountersInfo(context, li)

        li = context.createControl(New li())
        ul.addchild(li)
        xmltext = context.createControl(New xmltext())
        xmltext.text = "public ips info"
        li.addchild(xmltext)
        addpublicIpsInfo(context, li)

        li = context.createControl(New li())
        ul.addchild(li)
        xmltext = context.createControl(New xmltext())
        xmltext.text = "original request ips info"
        li.addchild(xmltext)
        addoriginalIpsInfo(context, li)

        Return rtn
    End Function

    Protected Overrides Function processImpl(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        control = createXHtmlPage(context)
        Return True
    End Function
End Class
